<template>
	<div>
		<van-image
			style="display: block; margin: auto"
			fit="contain"
			height="100vh"
			:src="baseUrl + data.AttCover"
			v-if="
				data.AttExt == '.jpg' ||
				data.AttExt == '.jpeg' ||
				data.AttExt == '.png' ||
				data.AttExt == '.gif'
			"
		/>
		<div class="read-pdf" v-else>
			<pdf ref="pdf" v-for="i in numPages" :key="i" :src="url" :page="i"></pdf>
		</div>
	</div>
</template>

<script>
import { getAttCode } from '@/api/cert'
import pdf from 'vue-pdf'
export default {
		props: {
		code: {
			type: String,
			required: true,
		},
    },
	components: {
		pdf,
	},
	data() {
		return {
			numPages: null, // pdf 总页数
			form: {
				attCode: '',
			},
			data: {},
			baseUrl: process.env.VUE_APP_BASE_API,
			url: '',
		}
	},
	created() {
		this.form.attCode = this.code
		this.getAttCode()
	},
	mounted() {},
	methods: {
		getAttCode() {
			getAttCode(this.form).then((res) => {
				this.data = res.data
				this.url = this.baseUrl + res.data.AttMiddlePath
				this.getNumPages()
			})
		},
		// # 计算pdf页码总数
		getNumPages() {
			let loadingTask = pdf.createLoadingTask(this.url)
			loadingTask.promise
				.then((pdf) => {
					this.numPages = pdf.numPages
				})
				.catch((err) => {
					console.error('pdf 加载失败', err)
				})
		},
	},
}
</script>

<style lang="less" scoped>
</style>